using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Threading.Tasks;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;

namespace LynxWebLog.Areas.Pages.Controllers
{
    public class PageController : Controller
    {
        private string _pageOwner;
        private string _blogName;
        private BlogSupport blogSupport;
        protected override void Initialize(RequestContext requestContext)
        {
            ViewBag.Version = " v: " + Assembly.GetExecutingAssembly().GetName().Version;
            base.Initialize(requestContext);
            this._pageOwner = this.ControllerContext.RouteData.Values["username"].ToString();
            this._blogName = this.ControllerContext.RouteData.Values["blogname"].ToString();
            this.blogSupport = new BlogSupport();
        }
        public async Task<ActionResult> ViewPage(string pgName)
        {
            PageData bEntry = await blogSupport.GetCurrentPage(_pageOwner, _blogName, pgName);
            ViewBag.LicenseHtml = CC_License.GetLicense(bEntry.license);
            ViewBag.PageOwner = this._pageOwner;
            ViewBag.BlogName = this._blogName;

            return View("PagePost", bEntry);
        }

        public async Task<ActionResult> Index(string sort = "Name")
        {
            List<PageData> pdIndex = await blogSupport.GetPageIndex(_pageOwner, _blogName);
            ViewBag.PageOwner = this._pageOwner;
            ViewBag.BlogName = this._blogName;
            if (sort.Equals("Name")) {
                pdIndex = pdIndex.OrderBy(pd => pd.title).ToList();
            } else {
                pdIndex = pdIndex.OrderByDescending(pd => pd.DateCreated).ToList();
            }

            return View("Index", pdIndex);
        }

	}
}